package dbutil

import (
	"fmt"
	"github.com/aceld/zinx/utils"
	log "github.com/cihub/seelog"
	"github.com/gomodule/redigo/redis"
	"time"
)

var redisClientsPool *redis.Pool

func DestroyRedisPool() {
	redisClientsPool.Close()
}

func InitRedisPool() error {

	redisClientsPool = &redis.Pool{
		//连接方法
		Dial: func() (redis.Conn,error) {
			c,err := redis.Dial("tcp",utils.GlobalObject.RedisURL)
			if err != nil {
				return nil,err
			}
			if _, err = c.Do("AUTH", utils.GlobalObject.RedisPassWord); err != nil {
				c.Close()
				return nil, err
			}
			c.Do("SELECT",1)
			return c,nil
		},
		//DialContext:     nil,
		//TestOnBorrow:    nil,
		//最大的空闲连接数，表示即使没有redis连接时依然可以保持N个空闲的连接，而不被清除，随时处于待命状态。
		MaxIdle:         1,
		//最大的激活连接数，表示同时最多有N个连接
		MaxActive:       10,
		//最大的空闲连接等待时间，超过此时间后，空闲连接将被关闭
		IdleTimeout:     180 * time.Second,
		//Wait:            false,
		//MaxConnLifetime: 0,
	}

	return nil
}

// 返回名称为key的hash中是否存在
func Hashhexists(key string, field string) (int64,error){

	redisC := redisClientsPool.Get()
	defer redisC.Close()
	val, err := redis.Int64(redisC.Do("HEXISTS",key,field))
	if err != nil {
		log.Error("Redis HashGet Error:", err)

	}
	return val,nil
}

// 返回名称为key的hash中field对应的value
func HashGet(key string, field string) (string,error) {

	redisC := redisClientsPool.Get()
	defer redisC.Close()
	val, err := redis.String(redisC.Do("hget", key, field))
	if err != nil {
		log.Error("Redis HashGet Error:", err)
		return "",err
	}
	return val,nil
}

func KEYEXISTS(key string) (int64,error){

	redisC := redisClientsPool.Get()
	defer redisC.Close()
	val, err := redis.Int64(redisC.Do("EXISTS",key))
	if err != nil {
		log.Error("Redis HashGet Error:", err)

	}
	return val,nil
}

func StringSet(key string,data interface{}) error {
	redisC := redisClientsPool.Get()
	defer redisC.Close()
	reply,err :=  redis.String(redisC.Do("set",key,data))
	if err !=nil{
		log.Error("Redis StringSet Error:", err)
	}
	fmt.Print(reply)

	return err
}

func StringGetString(key string) (string,error) {

	redisC := redisClientsPool.Get()
	defer redisC.Close()
	reply,err :=  redis.String(redisC.Do("Get",key))
	if err !=nil{
		log.Error("Redis StringSet Error:", err)
		return "", err
	}
	fmt.Print(reply)

	return reply,nil

}

func StringGetFloat(key string) (float64,error) {

	redisC := redisClientsPool.Get()
	defer redisC.Close()
	reply,err :=  redis.Float64(redisC.Do("Get",key))
	if err !=nil{
	log.Error("Redis StringSet Error:", err)
	return 0, err
	}
	fmt.Print(reply)

	return reply,nil
}

func StringGetInt64(key string) (int64,error){

	redisC := redisClientsPool.Get()
	defer redisC.Close()
	reply,err :=  redis.Int64(redisC.Do("Get",key))
	if err !=nil{
		log.Error("Redis StringSet Error:", err)
		return 0, err
	}
	fmt.Print(reply)

	return reply,nil
}


// 向key的hash中添加元素field的值
func HashSet(key string, field string, data interface{}) error {

	redisC := redisClientsPool.Get()
	defer redisC.Close()
	reply, err := redis.Int64(redisC.Do("hset",key, field, data))
	if err != nil {
		log.Error("Redis HSet Error:", err)
	}
	fmt.Println(reply)
	return err
}

//获取hash下所有key返回list
func HashGetKeys(key string) []string {
	redisC := redisClientsPool.Get()
	defer redisC.Close()
	reply, err := redis.Strings(redisC.Do("hkeys",key))
	if err != nil{
		log.Error("Redis HashGetKeys Error:", err)
	}
	return reply
}




